import { postJSON, get, putJSON, patchJSON } from '@/utils/request'
import { useCounterStore } from '@/stores/modules/user'
import { type UserInfo } from '@/types'
import type { UploadFile } from 'element-plus'
export const reg = (registerUser: { username: string; password: string; repassword: string }) => {
  return postJSON('/api/reg', registerUser)
}

export const login = async (loginUser: { username: string; password: string }) => {
  const result = await postJSON('/api/login', loginUser)
  const userStore = useCounterStore()
  userStore.setToken(result.token)
  return result
}

export const getUserInfo = async (): Promise<UserInfo> => {
  const userStore = useCounterStore()
  if (userStore.userInfo) {
    return userStore.userInfo
  }
  const data = await get('/my/userinfo', undefined)
  userStore.setUserInfo(data.data)
  return userStore.userInfo!
}

export const updateInfo = (userInfo: UserInfo) => {
  putJSON('/my/userinfo', userInfo)
}

export const updateAvatar = (avatar: string) => {
  patchJSON('/my/update/avatar', { avatar })
}

export const updatePassword = (old_pwd: string, new_pwd: string, re_pwd: string) => {
  patchJSON('/my/updatepwd', { old_pwd: old_pwd, new_pwd: new_pwd, re_pwd: re_pwd })
}
